﻿using System;
using System.Collections.Generic;
using System.Linq;
using Web.Models;
using Model.Core.DataBase;
using Web.Models.AnalysisData;
using Web.Models.CoreData;


namespace Web.BML.CoreData
{
    /// <summary>
    /// 报文模块
    /// </summary>
    public class ReportOp
    {

        private static ReportOp instance;
        /// <summary>
        /// 构造函数
        /// </summary>
        private ReportOp()
        {

        }
        /// <summary>
        /// 单例模式实例化
        /// </summary>
        /// <returns></returns>
        public static ReportOp GetInstance()
        {
            if (instance == null)
            {
                instance = new ReportOp();
            }
            return instance;
        }

        

        /// <summary>
        /// 查询所有报文列表
        /// </summary>
        /// <returns></returns>
        public List<Report> QueryAllReportList()
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Report>().ToList();
            }
        }

        /// <summary>
        /// 查询时间段的报文
        /// </summary>
        /// <returns></returns>
        public List<Report> QueryReportList(string mmsi,long start,long end)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                return db.Queryable<Report>().Where(m=>mmsi.Contains(m.MMSI) && m.ReportTime>start &&m.ReportTime<end).ToList();
            }
        }

        /// <summary>
        /// 更新所有报文数据的船名和报文时间
        /// </summary>
        /// <returns></returns>
        public void ModifReports(List<Report> reportList)
        {
            using (IDbContext db = ContainerFactory.OpenCoreConnect())
            {
                foreach (var report in reportList)
                {
                    Report model = db.Queryable<Report>().Where(m => m.Key == report.Key).FirstOrDefault();
                    if (model == null)
                    {
                        continue;
                    }
                    else
                    {
                        
                        model.ShipName = report.ShipName;
                        model.ReportTime = report.ReportTime;
                        db.GetOperator<Report>().Modify(model);
                    }
                }
                db.SaveAllChange();

            }
        }

    }


}

